#import "OmBirthdayView.h"
@interface OmBirthdayView ()
{
    UIView *_contentView;
    UIView *_dismissView;
}
@property (nonatomic, strong) UIDatePicker *pickerView;
@end
@implementation OmBirthdayView
- (instancetype)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self _layoutMainView];
    }
    return self;
}
+ (OmBirthdayView *)showInWindowWithBlock:(DateSelectBlock)dateBlock {
    OmBirthdayView *birthday = [[OmBirthdayView alloc]init];
    birthday.dateBlock = dateBlock;
    [[UIApplication sharedApplication].keyWindow addSubview:birthday];
    [birthday showInView];
    return birthday;
}
#pragma mark - 业务处理
- (void)cancelHandler {
    [self hiddenInView];
}
- (void)sureHandler {
    NSDate *date = self.pickerView.date;
    if (self.dateBlock) {
        self.dateBlock(date);
    }
    [self hiddenInView];
}
- (void)showInView {
    self.hidden = NO;
    CGRect contentViewFrame = _contentView.frame;
    contentViewFrame.origin.y = CGRectGetHeight(self.bounds) - contentViewFrame.size.height - [self viewSafeAreaInsets].bottom;
    [UIView animateWithDuration:0.2 animations:^{
        self->_dismissView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        self->_contentView.frame = contentViewFrame;
    }];
}
- (void)hiddenInView {
    CGRect contentViewFrame = _contentView.frame;
    contentViewFrame.origin.y = self.bounds.size.height;
    [UIView animateWithDuration:0.2 animations:^{
        self->_dismissView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        self->_contentView.frame = contentViewFrame;
    }completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}
- (void)_layoutMainView {
    self.backgroundColor = [UIColor clearColor];
    _dismissView = [[UIView alloc]init];
    [self addSubview:_dismissView];
    _dismissView.frame = self.bounds;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelHandler)];
    [_dismissView addGestureRecognizer:tap];
    _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds), SCREEN_WIDTH, 280)];
    UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:_contentView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
    cornerRadiusLayer.frame = _contentView.bounds;
    cornerRadiusLayer.path = cornerRadiusPath.CGPath;
    _contentView.layer.mask = cornerRadiusLayer;
    _contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentView];
    [self addPickView];
    [self addHeaderView];
}
- (void)addPickView {
    _pickerView = [[UIDatePicker alloc] init];
//     [_pickerView setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    _pickerView.datePickerMode = UIDatePickerModeDate;
    _pickerView.backgroundColor = [UIColor whiteColor];
    [_pickerView setDate:[NSDate date] animated:YES];
    [_pickerView setMaximumDate:[NSDate date]];
    [_contentView addSubview:_pickerView];
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self->_contentView);
        make.height.mas_equalTo(224);
    }];
}
- (void)addHeaderView {
    UIView *headerView = [[UIView alloc]init];
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom].fp_addTargetActionForControlEvents(self, @selector(cancelHandler), UIControlEventTouchUpInside);
    [cancleBtn setTitle:ASLocalizedString(@"取消") forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancleBtn setTitleColor:HexadecimalColor(@"#999999") forState:UIControlStateNormal];
    [headerView addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(headerView);
        make.bottom.equalTo(headerView).with.offset(-1);
        make.width.mas_equalTo(70);
    }];
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom].fp_addTargetActionForControlEvents(self, @selector(sureHandler), UIControlEventTouchUpInside);
    [sureBtn setTitle:ASLocalizedString(@"确认") forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sureBtn setTitleColor:HexadecimalColor(@"#FA5A8C") forState:UIControlStateNormal];
    [headerView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headerView);
        make.bottom.top.equalTo(cancleBtn);
        make.width.mas_equalTo(70);
    }];
    UIView *lineView = [[UIView alloc]init].fp_backgroundColor(HexadecimalColor(@"#E6E6E6"));
    [headerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(headerView);
        make.height.mas_equalTo(1);
    }];
    [_contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.left.top.right.equalTo(self->_contentView);
    }];
}
- (UIEdgeInsets)viewSafeAreaInsets {
    if (@available(iOS 11.0, *)) {
        return self.safeAreaInsets;
    } else {
        return UIEdgeInsetsZero;
    }
}
@end
